//
//  HSYAnimationHelper.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/4.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYAnimationHelper.h"
#import "POP.h"

static HSYAnimationHelper *hsyAnimationHelper;

@implementation HSYAnimationHelper

+ (instancetype)shareInstance {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hsyAnimationHelper = [[HSYAnimationHelper alloc] init];
    });
    return hsyAnimationHelper;
}

- (instancetype)init {
    
    if (self = [super init]) {
        NSLog(@"HSYAnimationHelper");
    }
    return self;
}


#pragma mark - System Controller Animation

- (void)customSystemAnimationForLayer:(CALayer *)layer duration:(CGFloat)duration subtype:(NSString *)subtype type:(NSString *)type key:(NSString *)key compaletion:(void(^)())compaletion {
    
    CATransition * transition = [CATransition animation];
    transition.duration = duration;
    
    transition.subtype = subtype;
    transition.type = type;
    
    [layer addAnimation:transition forKey:key];
    compaletion();
}

- (void)customSystemAnimationWithType:(kSystemAnimationType)animation withLayer:(CALayer *)layer duration:(CGFloat)duration subtype:(NSString *)subtype key:(NSString *)key compaletion:(void(^)())compaletion {
    
    [self customSystemAnimationForLayer:layer duration:duration subtype:subtype type:[self conversionSystemAnimationForType:animation] key:key compaletion:^{
        compaletion();
    }];
}


- (NSString *)conversionSystemAnimationForType:(kSystemAnimationType)type {
    
    NSString *typeString = nil;
    switch (type) {
        case kSystemAnimationTypeCube:
            typeString = kCube;
            break;
            
        case kSystemAnimationTypeFade:
            typeString = kCATransitionFade;
            break;
            
        case kSystemAnimationTypeOpen:
            typeString = kOpen;
            break;
            
        case kSystemAnimationTypePush:
            typeString = kCATransitionPush;
            break;
            
        case kSystemAnimationTypeSwirl:
            typeString = kSwirl;
            break;
            
        case kSystemAnimationTypeTubey:
            typeString = kTubey;
            break;
            
        case kSystemAnimationTypeTwist:
            typeString = kTwist;
            break;
            
        case kSystemAnimationTypeMoveIn:
            typeString = kMoveIn;
            break;
            
        case kSystemAnimationTypeReveal:
            typeString = kReveal;
            break;
            
        case kSystemAnimationTypeOglFlip:
            typeString = kOglFlip;
            break;
            
        case kSystemAnimationTypeZoomyIn:
            typeString = kZoomyIn;
            break;
            
        case kSystemAnimationTypePageCurl:
            typeString = kPageCurl;
            break;
            
        case kSystemAnimationTypeZoomyOut:
            typeString = kZoomyOut;
            break;
            
        case kSystemAnimationTypePageUnCurl:
            typeString = kPageUnCurl;
            break;
            
        case kSystemAnimationTypeSpewEffect:
            typeString = kSpewEffect;
            break;
            
        case kSystemAnimationTypeSuckEffect:
            typeString = kSuckEffect;
            break;
            
        case kSystemAnimationTypeGenieEffect:
            typeString = kGenieEffect;
            break;
            
        case kSystemAnimationTypeCharminUltra:
            typeString = kCharminUltra;
            break;
        
        case kSystemAnimationTypeRippleEffect:
            typeString = kRippleEffect;
            break;
            
        case kSystemAnimationTypeUnGenieEffect:
            typeString = kUnGenieEffect;
            break;
            
        case kSystemAnimationTypeCameraIrisHollowOpen:
            typeString = kCameraIrisHollowOpen;
            break;
            
        case kSystemAnimationTypeCameraIrisHollowClose:
            typeString = kCameraIrisHollowClose;
            break;
            
        case kSystemAnimationTypeOglApplicationSuspend:
            typeString = kOglApplicationSuspend;
            break;
            
        default:
            break;
    }
    
    return typeString;
}

#pragma mark - POP Animation

- (void)customPOPAnimationForLayer:(CALayer *)layer withPropertyName:(NSString *)propertyName toValue:(NSValue *)toValue fromValue:(NSValue *)fromValue springBounciness:(CGFloat)springBounciness springSpeed:(CGFloat)springSpeed {
    
    POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:propertyName];
    
    springAnimation.fromValue = fromValue;                      //
    springAnimation.toValue = toValue;                          //执行动画的值
    springAnimation.springBounciness = springBounciness;        //弹性值
    springAnimation.springSpeed = springSpeed;                  //弹性速度
    
    [layer pop_addAnimation:springAnimation forKey:NULL];
}


- (void)customPOPZoomInAnimationForLayer:(CALayer *)layer toValue:(NSValue *)toValue fromValue:(NSValue *)fromValue springBounciness:(CGFloat)springBounciness springSpeed:(CGFloat)springSpeed {
    
    [self customPOPAnimationForLayer:layer withPropertyName:kPOPViewSize toValue:toValue fromValue:fromValue springBounciness:springBounciness springSpeed:springSpeed];
}

- (void)customPOPEjectAnimationForLayer:(CALayer *)layer toValue:(NSValue *)toValue fromValue:(NSValue *)fromValue springBounciness:(CGFloat)springBounciness springSpeed:(CGFloat)springSpeed {
    
    [self customPOPAnimationForLayer:layer withPropertyName:kPOPViewFrame toValue:toValue fromValue:fromValue springBounciness:springBounciness springSpeed:springSpeed];
}

- (void)customPOPFadeInAnimationInView:(UIView *)view toValue:(NSNumber *)value forKey:(NSString *)key {
    
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.toValue = value;
    
    [view pop_addAnimation:animation forKey:key];
}

- (void)customPOPZPositionAnimationForLayer:(CALayer *)layer toValue:(NSValue *)toValue fromValue:(NSValue *)fromValue springBounciness:(CGFloat)springBounciness springSpeed:(CGFloat)springSpeed {
    
    [self customPOPAnimationForLayer:layer withPropertyName:kPOPLayerZPosition toValue:toValue fromValue:fromValue springBounciness:springBounciness springSpeed:springSpeed];
}
@end
